package com.swanclouds.api;

import com.swanclouds.api.account.AccountService;
import com.swanclouds.api.chassis.ChassisResourceService;
import com.swanclouds.api.event.EventResourceService;
import com.swanclouds.api.managers.ManagerResourceSerice;
import com.swanclouds.api.systems.SystemResourceService;
import com.swanclouds.api.task.TaskResourceService;
import com.swanclouds.api.updates.UpdateService;

import java.util.ServiceLoader;

public class Apis {

    private static final APIProvider provider = initializeProvider();

    /**
     * Gets the API implementation based on Type
     *
     * @param <T> the API type
     * @param api the API implementation
     * @return the API implementation
     */
    public static <T> T get(Class<T> api) {
        return provider.get(api);
    }

    private static APIProvider initializeProvider() {
        APIProvider p = ServiceLoader.load(APIProvider.class, Apis.class.getClassLoader()).iterator().next();
        p.initialize();
        return p;
    }

    public static SystemResourceService Systems() {
        return Apis.get(SystemResourceService.class);
    }

    public static ChassisResourceService Chassis() {
        return Apis.get(ChassisResourceService.class);
    }

    public static ManagerResourceSerice Managers() {
        return Apis.get(ManagerResourceSerice.class);
    }

    public static UpdateService Updates() {
        return Apis.get(UpdateService.class);
    }

    public static AccountService Accounts() {
        return Apis.get(AccountService.class);
    }

    public static TaskResourceService Tasks() {
        return Apis.get(TaskResourceService.class);
    }

    public static EventResourceService Events() {
        return Apis.get(EventResourceService.class);
    }
}